<?php
/**
 * CompleteFormView Registration
 *
 * @version    1.0
 * @package    samples
 * @subpackage tutor
 * @author     Pablo Dall'Oglio
 * @copyright  Copyright (c) 2006-2011 Adianti Solutions Ltd. (http://www.adianti.com.br)
 * @license    http://www.adianti.com.br/framework-license
 */
class CompleteFormView extends TPage
{
    private $form; // form
    
    /**
     * Class constructor
     * Creates the page and the registration form
     */
    function __construct()
    {
        parent::__construct();
        
        // creates the form
        $this->form = new TForm('form_City');
        
        // creates a table
        $table = new TTable;
        $table_buttons = new TTable;
        
        // add the table inside the form
        $this->form->add($table);
        
        // create the form fields
        $id   = new TEntry('id');
        $name = new TEntry('name');
        
        // define the sizes
        $id->setSize(100);
        $name->setSize(100);
        $id->setEditable(FALSE);
        
        // add a row for the field id
        $row=$table->addRow();
        $row->addCell(new TLabel('id:'));
        $row->addCell($id);

        // add a row for the field name
        $row=$table->addRow();
        $row->addCell(new TLabel('name:'));
        $row->addCell($name);

        // create an action button (save)
        $save_button=new TButton('save');
        // define the button action
        $save_button->setAction(new TAction(array($this, 'onSave')), 'Save');
        $save_button->setImage('ico_save.png');
        
        // create an action button (new)
        $new_button=new TButton('new');
        // define the button action
        $new_button->setAction(new TAction(array($this, 'onEdit')), 'New');
        $new_button->setImage('ico_new.png');
        
        // create an action button (go to list)
        $goto_button=new TButton('list');
        // define the button action
        $goto_button->setAction(new TAction(array('CompleteDataGridView', 'onReload')), 'Listing');
        $goto_button->setImage('ico_datagrid.gif');

        // add a row for the form action
        $row=$table_buttons->addRow();
        $row->addCell($save_button);
        $row->addCell($new_button);
        $row->addCell($goto_button);
        
        // add a row for the form action
        $row=$table->addRow();
        $cell=$row->addCell($table_buttons);
        $cell->colspan=2;

        // define wich are the form fields
        $this->form->setFields(array($id,$name,$save_button, $new_button, $goto_button));

        // add the form to the page
        parent::add($this->form);
    }

    /**
     * method onSave()
     * Executed whenever the user clicks at the save button
     */
    function onSave()
    {
        try
        {
            // open a transaction with database 'samples'
            TTransaction::open('samples');
            // get the form data into an active record City
            $object = $this->form->getData('City');
            // stores the object
            $object->store();
            
            // fill the form with the active record data
            $this->form->setData($object);
            
            // close the transaction
            TTransaction::close();
            // shows the success message
            new TMessage('info', 'Record saved');
            // reload the listing
        }
        catch (Exception $e) // in case of exception
        {
            // shows the exception error message
            new TMessage('error', '<b>Error</b> ' . $e->getMessage());
            // undo all pending operations
            TTransaction::rollback();
        }
    }
    
    /**
     * method onEdit()
     * Executed whenever the user clicks at the edit button da datagrid
     */
    function onEdit($param)
    {
        try
        {
            if (isset($param['key']))
            {
                // get the parameter $key
                $key=$param['key'];
                // open a transaction with database 'samples'
                TTransaction::open('samples');
                // instantiates object City
                $object = new City($key);
                // fill the form with the active record data
                $this->form->setData($object);
                
                // close the transaction
                TTransaction::close();
            }
            else
            {
                $this->form->clear();
            }
        }
        catch (Exception $e) // in case of exception
        {
            // shows the exception error message
            new TMessage('error', '<b>Error</b> ' . $e->getMessage());
            // undo all pending operations
            TTransaction::rollback();
        }
    }
}
?>